<template>
  <app-dialog v-model="show" width="600px" :title="parentdata.label" @open="handleOpen">
    <el-form
      ref="formRef"
      :model="formData"
      label-width="100px"
      label-position="right"
      style="width: 70%; margin: 0 auto"
    >
      <app-form-group v-model="formData" :json="formJson" />
    </el-form>

    <template #footer>
      <el-button @click="handleClose">取消</el-button>
      <el-button type="primary" class="hovepcvicecolor" @click="handleSubmit">确定</el-button>
    </template>
  </app-dialog>
</template>
    <script setup>
import { ElMessage } from 'element-plus'
import { http } from '@/utils/http'
import { http as uchttp } from '@/utils/uc-http'

import {
  getBctypedata,
  getGrouplist,
  getMoneylist,
  getSourcelist,
  getAgentParentagentlist,
  getProxyipListbytype,
} from '@/api/setting'
import { onMounted } from 'vue'

const userStore = useUserStore()
const show = defineModel({ type: Boolean })
const emits = defineEmits(['change'])
const props = defineProps({
  data: Object,
  parentdata: Object,
})

const formRef = ref(null)
const formData = ref({})

const bctypelist = ref([]) //平台列表
const grouplist = ref([]) //分组列表
const moneylist = ref([]) //货币列表
const sourcelist = ref([]) //来源列表

const parentagentlist = ref([]) //上级代理列表
const proxyiplist = ref([]) //代理IP列表

const plantcombinslist = ref([])
const plantgroupslist = ref([])

const watchplatformid = ref(null)

const timeoption = computed(() => {
  let json = []

  for (let i = 0; i < 24; i++) {
    let a = {
      value: i,
      label: `${i} 点`,
    }
    json.push(a)
  }
  return json
})
const required = true
const formJson = computed(() => {
  let json = []
  // grouping
  if (props.parentdata.value == 'source') {
    json = [
      { name: 'name', label: '来源名称', required },
      { name: 'contact', label: '联系方式', required },
      { name: 'remark', label: '备注', required },
      {
        name: 'status',
        label: '是否启用',
        required,
        type: 'select',
        options: [
          { label: '启用', value: 1 },
          { label: '禁用', value: 0 },
        ],
        inputAttr: {
          'controls-position': 'right',
          style: 'width:60%',
        },
      },
    ]
  } else if (props.parentdata.value == 'grouping') {
    json = [
      { name: 'name', label: '分组名称', required },
      // {
      //   name: 'type',
      //   label: '分组类型',
      //   required,
      //   type: 'select',
      //   options: [
      //     { label: '代理-业务组', value: '1' },
      //     { label: '代理-对盘组(对冲)', value: '2' },
      //     { label: '代理-订单来源组', value: '3' },
      //     { label: '代理-自定义组', value: '4' },
      //     { label: '平台-业务组', value: '5' },
      //     { label: '平台-对盘组(对冲)', value: '6' },
      //     { label: '平台-订单来源组', value: '7' },
      //     { label: '平台-自定义组', value: '8' },
      //     { label: '第三方用户-自定义组', value: '9' },
      //   ],
      //   inputAttr: {
      //     'controls-position': 'right',
      //     style: 'width:60%',
      //   },
      // },
      { name: 'remark', label: '备注', required },
    ]
  } else if (props.parentdata.value == 'proxyip') {
    // 代理池新增
    if (props.parentdata.status == 'create') {
      json = [
        { name: 'ip', label: 'IP地址', required },
        {
          name: 'expire_time',
          label: '过期时间',
          required,
          type: 'datetime',

          inputAttr: {
            'controls-position': 'right',
            style: 'width:60%',
          },
        },
        { name: 'remark', label: '备注', required },
      ]
    } else {
      // 代理池修改
      json = [
        { name: 'ip', label: 'IP地址', required },
        {
          name: 'expire_time',
          label: '过期时间',
          required,
          type: 'datetime',

          inputAttr: {
            'controls-position': 'right',
            style: 'width:60%',
          },
        },
        { name: 'remark', label: '备注', required },
        {
          name: 'status',
          label: '状态',
          required,
          type: 'select',
          options: [
            { label: '未使用', value: 0 },
            { label: '使用中', value: 1 },
            { label: '已过期', value: 2 },
          ],
          inputAttr: {
            'controls-position': 'right',
            style: 'width:60%',
          },
        },
      ]
    }
  } else if (props.parentdata.value == 'login') {
    json = [
      { name: 'name', label: '规则名称', required },
      {
        name: 'type',
        label: '规则类型',
        required,
        type: 'select',
        options: [
          { label: '自动登录登出', value: 1 },
          { label: '保持登录', value: 2 },
        ],
        inputAttr: {
          'controls-position': 'right',
          style: 'width:60%',
        },
      },
      {
        name: 'login_time',
        label: '登录时间',
        required,
        type: 'select',
        options: timeoption.value,
        inputAttr: {
          'controls-position': 'right',
          style: 'width:60%',
        },
      },
      {
        name: 'logout_time',
        label: '登出时间',
        required,
        type: 'select',
        options: timeoption.value,
        inputAttr: {
          'controls-position': 'right',
          style: 'width:60%',
        },
      },
    ]
  } else if (props.parentdata.value == 'agent') {
    json = [
      {
        name: 'platform_id',
        label: '平台',
        required,
        type: 'select',
        options: bctypelist.value,
        inputAttr: {
          'controls-position': 'right',
          style: 'width:60%',
        },
      },
      {
        name: 'group_id',
        label: '分组',
        required,
        type: 'select',
        options: grouplist.value,
        inputAttr: {
          'controls-position': 'right',
          style: 'width:60%',
        },
      },
      {
        name: 'parent_agent_id',
        label: '上级代理',
        type: 'select',
        options: parentagentlist.value,
        inputAttr: {
          'controls-position': 'right',
          style: 'width:60%',
        },
      },
      {
        name: 'proxy_ip',
        label: '代理IP',
        required,
        type: 'select',
        options: proxyiplist.value,
        inputAttr: {
          'controls-position': 'right',
          style: 'width:60%',
        },
      },
      { name: 'user_name', label: '账号', required },
      { name: 'pass_word', label: '密码', required },
      { name: 'percent', label: '成数', required },
      { name: 'warn_amount', label: '预警金额', required },
      {
        name: 'money_code',
        label: '货币',
        required,
        type: 'select',
        options: moneylist.value,
        inputAttr: {
          'controls-position': 'right',
          style: 'width:60%',
        },
      },
      {
        name: 'source_id',
        label: '来源',
        required,
        type: 'select',
        options: sourcelist.value,
        inputAttr: {
          'controls-position': 'right',
          style: 'width:60%',
        },
      },
      { name: 'agent_amount', label: '代理限额', required },
      { name: 'members_amount', label: '成员限额', required },
      {
        name: 'status',
        label: '状态',
        required,
        type: 'select',
        options: [
          { value: 0, label: '准备-0' },
          { value: 1, label: '开启-1' },
          { value: 2, label: '暂停-2' },
          { value: 3, label: '关闭-3' },
        ],
        inputAttr: {
          'controls-position': 'right',
          style: 'width:60%',
        },
      },
      { name: 'remark', label: '备注', required },
    ]
  } else if (props.parentdata.value == 'authorize') {
    json = [
      {
        name: 'plant_code',
        label: '三方编码',
        required,
        type: 'select',
        options: plantcombinslist.value,
        inputAttr: {
          'controls-position': 'right',
          onChange: () => {
            getplantgroups(formData.value.plant_code)
          },
          style: 'width:100%',
        },
      },
      {
        name: 'sub_gid',
        label: '选择组织',
        required,
        type: 'select',
        options: plantgroupslist.value,
        inputAttr: {
          'controls-position': 'right',
          style: 'width:100%',
        },
      },
      {
        name: 'authority',
        label: '授权范围',
        required,

        type: 'select',
        options: [
          {
            value: 'memeber',
            label: '会员',
          },
          {
            value: 'ip',
            label: 'IP',
          },
          {
            value: 'order',
            label: '订单',
          },
        ],
        inputAttr: {
          multiple: true,
          'controls-position': 'right',
          style: 'width:100%',
        },
      },
    ]
  }

  return json
})

const urls = computed(() => {
  let json = {}
  if (props.parentdata.value == 'source') {
    json = {
      create: '/source/source_create',
      update: '/source/source_update',
      createisarray: false,
      updateisarray: false,
    }
  } else if (props.parentdata.value == 'grouping') {
    json = {
      create: '/group/group_create',
      update: '/group/group_update',
      createisarray: false,
      updateisarray: false,
    }
  } else if (props.parentdata.value == 'proxyip') {
    json = {
      create: '/proxyip/proxyip_create',
      update: '/proxyip/proxyip_update',
      createisarray: true,
      updateisarray: false,
    }
  } else if (props.parentdata.value == 'login') {
    json = {
      create: '/loginrule/loginrule_create',
      update: '/loginrule/loginrule_update',
      createisarray: false,
      updateisarray: false,
    }
  } else if (props.parentdata.value == 'authorize') {
    json = {
      create: '/accredit/create_plant_group',
      update: '/accredit/update_plant_group',
      createisarray: false,
      updateisarray: false,
    }
  }
  return json
})
const timetrans = inject('timetrans')

// 打开弹窗
const handleOpen = () => {
  console.log(props.parentdata)

  if (props.data.isTrusted != null && props.data.isTrusted != undefined) {
    formData.value = {}
  } else {
    if (props.data.expire_time) {
      props.data.expire_time = timetrans(props.data.expire_time)
    }
    formData.value = props.data
    if (props.parentdata.value == 'authorize' && formData.value.plant_code) {
      uchttp.post('/plant_groups', { plant_code: formData.value.plant_code }).then(res => {
        plantgroupslist.value = res.map(item => {
          return { value: item.gid, label: item.group_name }
        })
      })
    }
  }

  if (props.parentdata.value == 'authorize') {
    uchttp.post('/plant_combins', { plant_code: userStore.packageJs.plantcode }).then(res => {
      plantcombinslist.value = res.map(item => {
        return { value: item.plant_code, label: item.plant_name }
      })
    })
  }
}

const getplantgroups = code => {
  formData.value.sub_gid = ''
  uchttp.post('/plant_groups', { plant_code: code }).then(res => {
    plantgroupslist.value = res.map(item => {
      return { value: item.gid, label: item.group_name }
    })
  })
}

const changeplatformId = id => {
  let obj = bctypelist.value.find(obj => obj.value === id)
  if (obj) {
    let bctype = obj.bc_type
    // 获取上级代理列表
    getAgentParentagentlist({ platform_id: id }).then(res => {
      if (res.code == 200) {
        parentagentlist.value = res.data.map(item => {
          return { value: item.id, label: item.user_name }
        })
      }
    })

    // 获取代理IP列表
    // getProxyipListbytype({ page: 1, page_size: 100, bc_type: bctype }).then(res => {
    //   if (res.code == 200) {
    //     console.log(res);

    //     proxyiplist.value = res.data.list.map(item => {
    //       return { value: item.ip, label: item.ip }
    //     })
    //   }
    // })
  }
}

const handleClose = () => {
  show.value = false
}

// 保存
const handleSubmit = () => {
  formRef.value.validate().then(async () => {
    const body = {
      ...formData.value,
    }

    if (props.parentdata.value == 'agent') {
      if (formData.value.parent_agent_id == null || formData.value.parent_agent_id == undefined) {
        body.parent_agent_id = 0
      }
    }

    if (props.parentdata.value == 'authorize') {
      let pgnameObj = plantcombinslist.value.find(obj => obj.value === body.plant_code)
      body.pg_name = pgnameObj.label
      let subnameObj = plantgroupslist.value.find(obj => obj.value === body.sub_gid)
      body.sub_name = subnameObj.label
    }

    const onSuccess = () => {
      ElMessage.success('操作成功')
      handleClose()
      emits('change')
    }

    if (body.id) {
      // 更新
      let params
      if (urls.value.updateisarray) {
        params = [body]
      } else {
        params = body
      }

      http({
        method: 'post',
        url: urls.value.update,
        data: params,
      }).then(onSuccess)
    } else {
      // 创建

      let params
      if (props.parentdata.value == 'grouping') {
        body.status = 1
      } else if (props.parentdata.value == 'proxyip') {
        body.status = 0
      }

      if (urls.value.createisarray) {
        params = [body]
      } else {
        params = body
      }

      http({
        method: 'POST',
        url: urls.value.create,
        data: params,
      }).then(onSuccess)
    }
  })
}

onMounted(() => {})
</script>
    <style lang="scss" scoped></style>
    